package com.waterfairy.http.request;

import android.text.TextUtils;

import com.waterfairy.http.log.RequestLogUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by water_fairy on 2017/5/18.
 * 995637517@qq.com
 */

public class RequestInterceptor implements Interceptor {
    protected HashMap<String, String> params;
    protected boolean showUrl;
    private boolean addParams = false;

    public RequestInterceptor(boolean showUrl) {
        this.showUrl = showUrl;
    }

    public RequestInterceptor(boolean showUrl, boolean addParams) {
        this.showUrl = showUrl;
        this.addParams = addParams;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        if (addParams) params = getPopParams();
        Request request = chain.request();

        HttpUrl url = request.url();
        RequestBody body = request.body();

        if (body != null) {
            if (body instanceof FormBody) {
                //单类型
                body = addParamsToFormBody(url, (FormBody) body);
            } else if (body instanceof MultipartBody) {
                //多类型
                body = addParamsToMulFormBody(url, (MultipartBody) body);
            }
        } else {
            url = addParamsToUrl(url);
        }

        if (showUrl) RequestLogUtils.log(url, body);

        Request newRequest = request.newBuilder()
                .url(url)
                .method(request.method(), body)
                .build();
        return chain.proceed(newRequest);
    }

    protected HashMap<String, String> getPopParams() {
        return null;
    }


    /**
     * get请求添加数据
     *
     * @param url
     * @return
     */
    private HttpUrl addParamsToUrl(HttpUrl url) {
        String oriUrl = url.toString();
        StringBuilder stringBuilder = new StringBuilder(oriUrl.contains("?") ? oriUrl : (oriUrl + "?"));
        HashMap<String, String> tempParams = new HashMap<>();
        if (params != null && params.size() > 0) {
            Set<String> strings = params.keySet();
            for (String key : strings) {
                String value = params.get(key);
                stringBuilder.append("&").append(key).append("=").append(value);
                tempParams.put(key, value);
            }
        }

        Set<String> strings2 = url.queryParameterNames();
        for (String key : strings2) {
            String value = url.queryParameter(key);
            tempParams.put(key, value);
        }
        putExtraParamsToUrl(url, stringBuilder, tempParams);
        return HttpUrl.parse(stringBuilder.toString());
    }


    /**
     * 多类型 添加数据
     *
     * @param url
     * @param multipartBody
     * @return
     */
    private RequestBody addParamsToMulFormBody(HttpUrl url, MultipartBody multipartBody) {
        HashMap<String, String> tempParams = new HashMap<>();
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        if (params != null && params.size() > 0) {
            Set<String> strings = params.keySet();
            for (String key : strings) {
                if (!tempParams.containsKey(key)) {
                    String value = params.get(key);
                    builder.addFormDataPart(key, value);
                    tempParams.put(key, value);
                }
            }
        }
        for (int i = 0; i < multipartBody.size(); i++) {
            builder.addPart(multipartBody.part(i));
        }
        putExtraParamsToMulFormBody(url, builder, tempParams);
        return builder.build();
    }


    /**
     * 单form 添加数据
     *
     * @param url
     * @param formBody
     * @return
     */
    private FormBody addParamsToFormBody(HttpUrl url, FormBody formBody) {
        HashMap<String, String> tempParams = new HashMap<>();
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null && params.size() > 0) {
            Set<String> strings = params.keySet();
            for (String key : strings) {
                if (!tempParams.containsKey(key)) {
                    String value = params.get(key);
                    if (!TextUtils.isEmpty(value)) {
                        builder.add(key, value);
                        tempParams.put(key, value);
                    }
                }
            }
        }

        for (int i = 0; i < formBody.size(); i++) {
            String value = formBody.value(i);
            if (!TextUtils.isEmpty(value)) {
                String key = formBody.name(i);
                if (!tempParams.containsKey(key)) {
                    builder.add(key, value);
                    tempParams.put(key, value);
                }
            }
        }
        putExtraParamsToFromBody(url, builder, tempParams);
        return builder.build();
    }

    protected void putExtraParamsToFromBody(HttpUrl url, FormBody.Builder builder, HashMap<String, String> tempParams) {

    }

    protected void putExtraParamsToUrl(HttpUrl url, StringBuilder stringBuilder, HashMap<String, String> tempParams) {

    }

    protected void putExtraParamsToMulFormBody(HttpUrl url, MultipartBody.Builder builder, HashMap<String, String> tempParams) {

    }
}
